<?php
class Control_Gm_selectUserInfo {
	function selectUserInfo() {
		
		$action = MooForm::request('action');
		$view = MooForm::request('view');
		
		$gifts = MooConfig::get('gm_gift');
		$gm_awardsinfo = MooConfig::get('gm_awardstype.type');
		
		MooView::set('gm_awardsinfo', $gm_awardsinfo);
		$giftData = "";
		if($gifts) {
			foreach ($gifts as $key => $val) {
				if($giftData == "") {
					$giftData = $key;
				} else {
					$giftData .= "|" . $key ;
				}			
			}
		}
		
// 		$giftData = "hello|13612345564|13825646464|13412236054|13012348564|13012345564|13012365564|彭小|王达||王飞|李相公|王二|李四|李五|周欢欢|钻石100|钻石500|钻石1000|金币100|金币500|金币1000|";
		MooView::set('sourceData', $giftData);
		
		if(!$view) {
			$view = 1;
		}
		MooView::set('view', $view);
		
		// 允许修改权限
		MooView::set('permit', 1);
		
		$userOperation = MooConfig::get('useroperation.action');
		MooView::set('num', count($userOperation));
		MooView::set('userOperation', $userOperation);
		
		$serverOperation = MooConfig::get('serveroperation.action');
		
		MooView::set('num2', count($serverOperation));
		MooView::set('serverOperation', $serverOperation);

		//公告相关
        $url = MooConfig::get('main.url.allnotice');
        $res = MooUtil::curl_send($url, null, 1);
        $gonggaos = MooJson::decode($res, true);
        if($gonggaos){
            MooView::set('gonggaos', $gonggaos);
        }


        //区服状态
        $url = MooConfig::get('main.url.serversforstate');
        $res = MooUtil::curl_send($url, null, 1);
        $servers = MooJson::decode($res, true);
        foreach ($servers['servers'] as $key => $value){
            switch ($value['state']){
                case 1:
                    $servers['servers'][$key]['stateText'] = "<font style='color:grey'>维护中</font>";
                    break;
                case 2:
                    $servers['servers'][$key]['stateText'] = "<font style='color:green'>新服</font>";
                    break;
                case 3:
                    $servers['servers'][$key]['stateText'] = "<font style='color:red'>火爆</font>";
                    break;
                case 4:
                    $servers['servers'][$key]['stateText'] = "<font style='color:white'>白名单</font>";
                    break;
            }
        }
        if($servers){
            MooView::set('servers', $servers['servers']);
        }
        //邮件发送记录 默认一个月以内的
        $startTime = strtotime("-1 month");
        $awardLogDao =  MooDao::get('AwardLog');
        $sendlogs = $awardLogDao->getAll("SELECT * FROM @TABLE WHERE send_time>".$startTime);
        if($sendlogs){
            foreach ($sendlogs as $key => $value){
                $sendlogs[$key]['send_time'] = date('Y-m-d H:i:s',$value['send_time']);
                if($value['playerid'] == '-1'){
                    $sendlogs[$key]['playerid'] = '全服玩家';
                }
                $curSendContentStr = '';
                if($value['award_info']){
                    $gm_awardsLists = MooConfig::get('gm_awardsinfo.giftIds');

                    $curSendContentsArr = explode(',',$value['award_info']);
                    foreach ($curSendContentsArr as $curSendContent){
                        $curSendContentArr = explode('_',$curSendContent);
                        foreach ($gm_awardsinfo as $awardstype){
                            if($curSendContentArr[0] == $awardstype['id']){
                                $curContentType = $awardstype['name'];
                                break;
                            }
                        }
                        foreach ($gm_awardsLists as $gm_awardsList){
                            if($gm_awardsList['type'] == $curSendContentArr[0] && $gm_awardsList['itemId'] == $curSendContentArr[1]){
                                $curContentName = $gm_awardsList['name'];
                            }
                        }
                        $curSendContentStr.="[".$curContentType."]";
                        $curSendContentStr.=$curContentName;
                        $curSendContentStr.="(数量:".$curSendContentArr[2].")<br/>";
                    }
                }else{
                    $curSendContentStr = '没有邮件附件';
                }
                $sendlogs[$key]['sendContentStr']  = $curSendContentStr;
            }
            MooView::set('sendlogs', $sendlogs);
        }else{
            MooView::set('sendlogs', array());
        }
        MooView::set('sendEndDate', date('Y-m-d',time()));
        MooView::set('sendStartDate', date('Y-m-d',strtotime("-1 month")));

        if($action == 0) {
			$nowGame = MooConfig::get('main.defaultGame');
			$servers = MooConfig::get('server.' . $nowGame);
			
			MooView::set('nowGame', $nowGame);
			MooView::render("gm_selectUserInfo");
		}else {
			
			$lang = MooConfig::get('lang.zh');
			
			$userName = MooObj::get('User')->getUserName();
			$getGame = $game 	= MooForm::request('game');
			// 设置平台信息
			$gameLangConf = MooConfig::get('game_lang.zh_en');
			if($gameLangConf[$game]) {
				$game = $gameLangConf[$game];
			}
			
			$channelInfo = MooObj::get('Game')->getPlatformByGame($game);
			
			if($channelInfo) {
				MooView::set('channelInfo', $channelInfo);
			}
			
			$type 	= MooForm::request('type');
			$role 	= MooForm::request('role');
			$puid 	= MooForm::request('puid');
			$id 	= MooForm::request('id');
			// playername/puid/playerid
			$params = array();
			if($type == "role") {
				$roleEncode = urlencode($role);
				$params['playername'] = $roleEncode;
				MooView::set('role', $role);
			} else if ($type == "puid") {
				$params['puid'] = $puid;
				MooView::set('puid', $puid);
			} else if($type == "id") {
				$params['playerid'] = $id;
				MooView::set('playerid', $id);
			}
			
			$server 	= MooForm::request('serverUrl');
			$serverLists = MooObj::get('Gm')->getServerListByGame($game);
			
			$channels = array();
			if($serverLists) {
				foreach($serverLists as $key=> $val) {
					if(!in_array($val['platform'], $channels)) {
				        	if($game == "qmby" && $val['platform'] == "appstore") {
				        	}else {
								$channels[] = $val['platform'];
							}
					}
					
				}
			}
			if($channels) {
				foreach($channels as $k => $v) {
					MooView::set('channel_'.$k,  $v);
				}
			}
			
			if($serverLists) {
				$serverUrl = $serverLists[$server]['url'];
			}
			
			$cmd = "playerinfo";
			if($params) {
				$url = MooObj::get('Gm')->getUrl($serverUrl, $cmd, $userName, $params);
				
				$data = MooUtil::curl_send($url);
				
			//	$data = '{"status":1,"id":1,"puid":"todd","name":"Milly.G","level":5,"exp":474,"golden":130,"vip":0,"silver":11394,"isOnline":"false"}';
				if ($data) {
					$dataArr = MooJson::decode($data, true);

					// 查询之后设置 playerid
					$playerid = $dataArr['id'];
					MooSession::set($userName . '_playerid', $playerid);
					
					$newData = array();
					foreach ($dataArr as $key => $val) {
						if($lang[$key]) {
							$arrData['keyMsg'] =  $lang[$key];
						} else {
							$arrData['keyMsg'] =  $key;
						}
						$arrData['key'] =  $key;
						$arrData['value'] =  $val;
						$newData[] = $arrData;
					}
					
					if($newData) {
						foreach ($newData as $k => $v) {
							if(in_array($v['key'], array('golden','vip','silver'))) {
								$newData[$k]['isChange'] = 1;
							} else {
								$newData[$k]['isChange'] = 0;
							}
						}
					}
					MooView::set('viewData', $newData);
				} else {
					$msg = "用户不存在";
					MooView::set('msg', $msg);
				}
			}
			
			MooView::set('nowGame', $getGame);
			MooView::set('nowServer', $server);
			MooView::set('serverLists', $serverLists);
			MooView::render("gm_selectUserInfo");
		}
	}
}
